package com.example.controller;

import com.example.dto.RanchPageDto;
import com.example.entity.Page;
import com.example.entity.Ranch;
import com.example.entity.Result;
import com.example.service.RanchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("ranch")
public class RanchController {

    @Autowired
    private RanchService ranchService;

    @GetMapping("list")
    public Result getAllRanch(Page page) {
        Result result = new Result();
        if (page != null) {
            int pageHelper = (page.getCurrentPage() - 1) * page.getPageSize();
            page.setCurrentPage(pageHelper);
        }
        List<Ranch> allRanch = ranchService.findAllRanch(page);
        List<Ranch> ranchList = ranchService.findAll();

        if (allRanch != null) {
            result.setCode(1);
            result.setMsg("查询成功!");
            result.setRanchData(allRanch);
            result.setTotal(ranchList.size());
        } else {
            result.setCode(0);
            result.setMsg("查询失败!");
        }
        return result;
    }

    @PostMapping("del")
    public Result delRanch(@RequestBody Ranch ranch) {
        Result result = new Result();
        // 删除用户方法1：通过字段state来设置用户是否删除
        int i = ranchService.delRanch(ranch);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("禁用成功!");
        } else {
            result.setCode(0);
            result.setMsg("禁用失败!");
        }
        return result;
    }

    @PostMapping("edit")
    public Result editRanch(@RequestBody Ranch ranch) {
        Result result = new Result();
        int i = ranchService.updateRanch(ranch);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("修改成功!");
        } else {
            result.setCode(0);
            result.setMsg("修改失败!");
        }
        return result;
    }

    @PostMapping("delete")
    public Result deleteRanch(@RequestBody Ranch ranch) {
        Result result = new Result();
        int i = ranchService.deleteRanch(ranch);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("删除成功!");
        } else {
            result.setCode(0);
            result.setMsg("删除失败!");
        }
        return result;
    }

    @PostMapping("search")
    public Result searchRanch(@RequestBody RanchPageDto ranchPageDto) {
        Result result = new Result();
        Page page = ranchPageDto.getPage();
        Ranch ranch = ranchPageDto.getRanch();

        if (page != null) {
            int pageHelper = (page.getCurrentPage() - 1) * page.getPageSize();
            page.setCurrentPage(pageHelper);
        }

        List<Ranch> ranchList = ranchService.searchRanch(page, ranch);
        List<Ranch> allRanch = ranchService.searchAllRanch(ranch);

        if (ranchList != null) {
            result.setCode(1);
            result.setMsg("查询成功，有" + allRanch.size() + "个结果!");
            result.setRanchData(ranchList);
            result.setTotal(allRanch.size());
        } else {
            result.setCode(0);
            result.setMsg("查询结果为空!");
        }
        return result;
    }

    @PostMapping("add")
    public Result addRanch(@RequestBody Ranch ranch) {
        Result result = new Result();
        int i = ranchService.addRanch(ranch);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("添加成功!");
        } else {
            result.setCode(0);
            result.setMsg("添加失败!");
        }
        return result;
    }

    @GetMapping("allranch")
    public Result getAllAvailableRanch() {
        Result result = new Result();
        List<Ranch> allAvailableRanch = ranchService.findAllAvailableRanch();
        if (allAvailableRanch != null) {
            result.setCode(1);
            result.setMsg("查询成功!");
            result.setRanchData(allAvailableRanch);
        } else {
            result.setCode(0);
            result.setMsg("查询失败!");
        }
        return result;
    }
}
